package cn.itsource.blog.controller;

import cn.itsource.blog.domain.Comment;
import cn.itsource.blog.service.ICommentService;
import cn.itsource.util.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：评论Comment
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论接口类")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @GetMapping("/{id}")
    public List<Comment> findByArticleId(@PathVariable("id")Long id){
        return commentService.findByArticleId(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")String id){
        try {
            commentService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param comment
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Comment comment){
        try {
           if(comment.getId()==null){
               commentService.add(comment);
           }else{
               commentService.update(comment);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

}
